#!/bin/env python
# encoding: gbk
import csv
import chardet
import urllib2
import bs4
import sys
import os
sys.defaultencoding = "gbk" 
TIME_OUT=10

url_base='http://trade.500.com/rcjc/index.php?lotid=10000&playid=43&expect=%s'
urlstring = url_base % sys.argv[1]
print urlstring

def convertstr(name):
	newname = name.encode('gbk','ignore')
	if len(newname) == len(name):
		name = name[0] + '  ' + name[3]
	return name

html=urllib2.urlopen(urlstring,timeout=TIME_OUT).read()
soup = bs4.BeautifulSoup(html,from_encoding="gb18030")
teams = soup.find('table',id='vsTable').findAll('tr')
if teams:
	for i in range(7,len(teams)):
		vsteams=teams[i]['data-vs']
		parts=vsteams.split(',')
		convertstr(parts[1])
		parts[0]=convertstr(parts[0])
		parts[1]=convertstr(parts[1])
		pypath = os.getcwd()
		os.system(pypath + '/octopus.py "'+  parts[0].encode('gbk') + \
				'" "' + parts[1].encode('gbk')+'"')
